package com.xj.edu.hotel.ExceptionHandler;

import java.util.List;

public final class ApiResultGenerator {

    /**
     *
     * @param flag
     * @param msg
     * @param result
     * @param url
     * @param rows
     * @param throwable
     * @return
     */
    public static ApiResult result(boolean flag,String msg,Object result,String url,int rows,Throwable throwable){
        ApiResult apiResult = ApiResult.newInstance();
        apiResult.setFlag(flag);
        apiResult.setMsg(msg==""?"执行成功":msg);
        apiResult.setResult(result);
        apiResult.setRows(rows);
        apiResult.setTime(System.currentTimeMillis());
        apiResult.setUrl(url);
        return apiResult;
    }

    /**
     * 执行成功后返回试图的方法
     * @param result
     * @return
     */
    public static ApiResult successResult(Object result){
        //rows  默认为0
        int rows = 0;
        //如果是集合
        if(result instanceof List){
            rows = ((List) result).size();
        }
        return result(true,"",result,"",rows,null);
    }

    /**
     * 执行失败后返回试图的方法
     * @param msg
     * @param throwable
     * @return
     */
    public static  ApiResult errorResult(String msg,Throwable throwable){
        return result(false,msg,"","",0,throwable);
    }

}
